package com.kb.erp.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kb.erp.common.ApiResponse;
import com.kb.erp.entity.quote.*;
import com.kb.erp.service.QuoteService;
import com.kb.erp.service.digi.DigikeyServiceImpl;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/quote")
@RestController
public class QuoteController {

    @Resource
    private QuoteService quoteService;

    @Resource
    private DigikeyServiceImpl digikeyService;


    @PostMapping("/query")
    public BaseResult query(@RequestBody QuoteTO to) throws JsonProcessingException {
        return  quoteService.query(to);
    }

    /**
     * 远程调用接口查询询价接口
     * @param to
     * @return
     */
    @PostMapping("/remote_api/query")
    public BaseResult<List<ComponentPriceVO>> queryRemoteApi(@RequestBody RemoteApiQuoteTO to) {
        return new BaseResult<List<ComponentPriceVO>>(quoteService.queryRemoteApi(to));
    }

    @GetMapping("statistics")
    public List<QuoteCount> statistics(){

        return quoteService.statistics();
    }


    @GetMapping("/updateDigikeyToken")
    @XxlJob("updateDigikeyToken")
//    @Scheduled(cron = "0 0/9 * * * ?")
    @ResponseBody
    public ApiResponse updateToken()  {
        digikeyService.updateToken();
        return ApiResponse.success("ok");
    }


    @GetMapping("/updateDigikeyTokenNew")
    @XxlJob("updateDigikeyTokenNew")
//    @Scheduled(cron = "0 0/29 * * * ?")
    @ResponseBody
    public ApiResponse updateTokenNew()  {
        digikeyService.updateTokenNew();
        return ApiResponse.success("ok");
    }




}
